package com.hlb.manager.controller;

import com.hlb.entity.Product;
import com.hlb.manager.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author hanlibin
 * @Description:产品
 * @date 2018/7/1121:59
 */
@RestController
@RequestMapping("/products")
public class ProductController {
    private static Logger Log=LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductService productService;

    @PostMapping("/addProduct")
    public Product addProduct(@RequestBody Product product) {
        Log.info("创建产品:参数：{}",product);

        Product result=productService.addProduct(product);

        Log.info("创建产品:结果：{}",result);

        return result;
    }

    @GetMapping("findOne/{id}")
    public Product findOne(@PathVariable String id) {
        Log.info("查询产品:参数：{}", id);
        Product result = productService.findOne(id);
        Log.info("创建产品:结果：{}", result);
        return result;

    }

    @GetMapping(value = "query")
    public Page<Product> query(String ids, BigDecimal minRewardRate, BigDecimal maxRewardRate, String status,
                               @RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        Log.info("查询产品,ids={},minRewardRate={},maxRewardRate={},status,pageNum={},pageSize={}");
        List<String> idList = null, statusList = null;
        if (!StringUtils.isEmpty(ids)) {
            idList = Arrays.asList(ids.split(","));
        }
        if (!StringUtils.isEmpty(status)) {
            statusList = Arrays.asList(status.split(","));
        }

        Pageable pageable = new PageRequest(pageNum, pageSize);
        Page<Product> page = productService.query(idList, minRewardRate, maxRewardRate, statusList, pageable);
        Log.info("查询产品,结果={}", page);
        return page;
    }
}
